
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:stripe_connection_plugin/stripe_connection_plugin.dart';


class StripeController extends GetxController {
  BuildContext? context;
  StripeConnectionPlugin stripeConnectionPlugin = StripeConnectionPlugin();
  var methodChannel = MethodChannel('stripe_connection_plugin');

  RxString connectionStatus = "".obs;
  RxString sn = "".obs;
  RxDouble batteryLevel = 0.0.obs;
  RxString log= "".obs;
  RxBool cashBoxConnection = false.obs;

  RxString cashBox = "".obs;
  @override
  void onInit() {
    stripeConnectionPlugin.connectionCallBack = (e){

      connectionStatus.value = e;

    };stripeConnectionPlugin.onBatteryLevelCallBack=(e){
      batteryLevel.value  = e;
      print("+++++++++++++++++++++++$batteryLevel");
    };
    /**插卡回调*/
    stripeConnectionPlugin.onLogCallBack=(e){
      log.value = e;
    };
    stripeConnectionPlugin.onCashBoxConnectionChange=(e){
      cashBoxConnection.value = e;
    };

    ever(sn, (callback) async{
      await init(sn.value);
    });
    ever(cashBoxConnection, (callback) async{
      if(!cashBoxConnection.value){
        /**连接钱箱*/
        stripeConnectionPlugin.cashBox(cashBox.value);
      }
    });
    ever(cashBox, (callback) async{
      // if(cashBox.value!=""){
        stripeConnectionPlugin.cashBox(cashBox.value);
      // }
    });
    ever(connectionStatus, (callback) {
      print(connectionStatus.value);
      print("ooooooooooooooooooooooooooooooooooo");
    });
  }

  void openCash(){
    stripeConnectionPlugin.openCash();
      // if(cashBoxConnection.value){
      //
      // }else{
      //   stripeConnectionPlugin.cashBox(cashBox.value);
      // }
  }


  Future<void> init(String sn) async{
    await stripeConnectionPlugin.init(sn,"tml_E0yYiA2ECKaz0s","https://gbf.ziotc.com/gbf-api/order/test/test/connectionToken");
  }
  void getCashBoxName() {
    cashBox.value = "zbox_F42D51842178";

  }
}
